package com.keepers.repair.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.keepers.repair.R;

/**
 * auth aboom by 2018/2/24.
 * 下拉刷新上拉加载recyclerView
 */

public class PullRecyclerView extends FrameLayout implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    /**
     * 自定义属性
     */
    private int     padding;
    private int     paddingLeft;
    private int     paddingRight;
    private int     paddingTop;
    private int     paddingBottom;
    private int     scrollbarStyle;
    private int     backgroundColor;
    private boolean clipToPadding;
    private boolean scrollbarNone;

    private OnRefreshListener  onRefreshListener;
    private OnLoadMoreListener onLoadMoreListener;

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView       recyclerView;
    private BaseQuickAdapter   adapter;

    private int currentPage = 1;
    private boolean loadMore; //是否加载更多
    private boolean isLastPage = true;

    public PullRecyclerView(Context context) {
        this(context, null);
    }

    public PullRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.view_recycler_layout, this);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PullRecyclerView);
        backgroundColor = typedArray.getColor(R.styleable.PullRecyclerView_recyclerBackgroundColor, -1);
        padding = (int) typedArray.getDimension(R.styleable.PullRecyclerView_recyclerPadding, -1.0f);
        paddingLeft = (int) typedArray.getDimension(R.styleable.PullRecyclerView_recyclerPaddingLeft, 0.0f);
        paddingRight = (int) typedArray.getDimension(R.styleable.PullRecyclerView_recyclerPaddingRight, 0.0f);
        paddingTop = (int) typedArray.getDimension(R.styleable.PullRecyclerView_recyclerPaddingTop, 0.0f);
        paddingBottom = (int) typedArray.getDimension(R.styleable.PullRecyclerView_recyclerPaddingBottom, 0.0f);
        scrollbarStyle = typedArray.getInt(R.styleable.PullRecyclerView_recyclerScrollbarStyle, 2);
        clipToPadding = typedArray.getBoolean(R.styleable.PullRecyclerView_recyclerClipToPadding, Boolean.FALSE);
        scrollbarNone = typedArray.getBoolean(R.styleable.PullRecyclerView_recyclerScrollbarNone, Boolean.TRUE);
        typedArray.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setEnabled(Boolean.FALSE);
        recyclerView = findViewById(R.id.recyclerView);
        swipeRefreshLayout.setColorSchemeResources(
                R.color.x_red,
                R.color.x_blue,
                R.color.x_yellow,
                R.color.x_green
        );
        swipeRefreshLayout.setOnRefreshListener(this);
        if (padding != -1) {
            recyclerView.setPadding(padding, padding, padding, padding);
        } else {
            recyclerView.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
        }
        recyclerView.setClipToPadding(clipToPadding);

        if (scrollbarNone) {
            recyclerView.setVerticalScrollBarEnabled(Boolean.FALSE);
            recyclerView.setHorizontalScrollBarEnabled(Boolean.FALSE);
        } else {
            recyclerView.setScrollBarStyle(scrollbarStyle);
        }
        if (backgroundColor != -1) {
            recyclerView.setBackgroundColor(backgroundColor);
        }
    }

    public void setLayoutManager(RecyclerView.LayoutManager layout) {
        if (layout == null) {
            return;
        }
        recyclerView.setLayoutManager(layout);
    }

    public void setAdapter(RecyclerView.Adapter adapter) {
        setAdapter(adapter, false);
    }

    public void setAdapter(RecyclerView.Adapter adapter, boolean isLoadMore) {
        if (adapter == null) {
            return;
        }
        if (!(adapter instanceof BaseQuickAdapter)) {
            throw new IllegalArgumentException("adapter没有继承 BaseQuickAdapter");
        }
        this.adapter = (BaseQuickAdapter) adapter;
        this.recyclerView.setAdapter(this.adapter);
        if (isLoadMore) {
            this.adapter.setOnLoadMoreListener(this, recyclerView);
            this.adapter.disableLoadMoreIfNotFullPage();
        }
    }


    @Override
    public void onRefresh() {
        currentPage = 1;
        adapter.setEnableLoadMore(Boolean.FALSE);//这里的作用是防止下拉刷新的时候还可以上拉加载
        if (onRefreshListener != null) {
            onRefreshListener.onRefresh();
        }
    }

    public void showRefresh() {
        swipeRefreshLayout.setRefreshing(Boolean.TRUE);
    }

    public void loadComplete(boolean isLastPage) {
        this.isLastPage = isLastPage;
        if (loadMore) {
            loadMore = Boolean.FALSE;
            if (this.isLastPage) {
                adapter.loadMoreEnd();
            } else {
                adapter.loadMoreComplete();
            }
        } else {
            adapter.setEnableLoadMore(Boolean.TRUE);
            swipeRefreshLayout.setRefreshing(Boolean.FALSE);
        }
    }

    public boolean loadMoreFail() {
        if (loadMore) {
            adapter.loadMoreFail();
            currentPage--;
            return Boolean.TRUE;
        } else {
            currentPage = 1;
            adapter.setEnableLoadMore(Boolean.TRUE);
            swipeRefreshLayout.setRefreshing(Boolean.FALSE);
            return Boolean.FALSE;
        }
    }

    @Override
    public void onLoadMoreRequested() {
        if (isLastPage) {
            adapter.loadMoreEnd();
            return;
        }
        loadMore = true;
        currentPage++;
        if (onLoadMoreListener != null) {
            onLoadMoreListener.onLoadMore(currentPage);
        }
    }

    public void setOnRefreshAndLoadMoreListener(OnRefreshAndLoadMoreListener onRefreshAndLoadMoreListener) {
        setOnRefreshListener(onRefreshAndLoadMoreListener);
        setOnLoadMoreListener(onRefreshAndLoadMoreListener);
    }

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
        this.swipeRefreshLayout.setEnabled(Boolean.TRUE);
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }


    public interface OnRefreshAndLoadMoreListener extends OnRefreshListener, OnLoadMoreListener {

    }

    public interface OnRefreshListener {
        void onRefresh();
    }

    public interface OnLoadMoreListener {
        void onLoadMore(int page);
    }

    public int getCurrentPage() {
        return currentPage;
    }
}
